﻿
using System.Windows;
using YY.Admin.ViewModels;
using YY.Admin.Views;
using YY.Admin.Core.SqlSugar;
using Microsoft.Extensions.Configuration;
using System.IO;
using YY.Admin.Setup;
using YY.Admin.EventBus;
using YY.Admin.Filter;
using YY.Admin.Core;
using YY.Admin.Module;
using Mapster;
using HandyControl.Data;
using HandyControl.Tools;
namespace YY.Admin
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        private IConfiguration _configuration;
        private ILoggerService _logger;
        protected override Window CreateShell()
        {
            return Container.Resolve<LoginWindow>();
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            // 构建配置
            _configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("./Configuration/appsettings.json", optional: false, reloadOnChange: true)
                .Build();
            // 全局配置
            TypeAdapterConfig.GlobalSettings.Default
                .IgnoreNullValues(true)
                .NameMatchingStrategy(NameMatchingStrategy.IgnoreCase);
            base.OnStartup(e);
        }
        //注册
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //后续调整为配置依赖注入（Prism Module）
            // 注册错误处理
            containerRegistry.RegisterSingleton<IErrorHandler, ErrorHandler>();
            //全局错误处理
            containerRegistry.RegisterSingleton<GlobalExceptionHandler>();

            // 注册事件聚合器（Prism自带）
            containerRegistry.RegisterSingleton<IEventAggregator, EventAggregator>();

            //项目配置选项
            containerRegistry.AddProjectOptions(_configuration);
            // 注册缓存服务
            containerRegistry.AddNewLifeCache(_configuration);
            //注册数据库服务
            containerRegistry.AddDbContext(_configuration);
            //注册通知事件服务
            containerRegistry.AddNotificationEventBus();
            // 服务注册
            containerRegistry.AddService();

            // 注册所有需要导航的视图
            containerRegistry.AddNavigation();
        }
        protected override void OnInitialized()
        {
            base.OnInitialized();
            // 获取日志服务
            _logger = Container.Resolve<ILoggerService>();
            // 初始化全局异常处理（通过解析触发构造函数注册）
            Container.Resolve<GlobalExceptionHandler>();
            _logger.Information("应用程序启动");
        }
        protected override void OnExit(ExitEventArgs e)
        {
            _logger.Information("应用程序退出");
            BaseViewModel.StopTokenCheckTimer();
            base.OnExit(e);
        }
    }

}
